package com.hm.health.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;

import com.goodix.iot.libbase.logger.Glog;
import com.hm.health.R;

import static android.content.Context.NOTIFICATION_SERVICE;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;


public class AppNotification {

    public static final String TAG = "AppNotification";
    public static final int NOTIFICATION_ID_1 = 1;

    private Context mContext = null;
    private NotificationManager mNotificationManager = null;
    private Notification mNotification = null;
    private NotificationManager notificationManager;

    public AppNotification(Context context) {
        Glog.d(TAG, "AppNotification builder");
        if (context != null) {
            this.mContext = context;
            mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        }
    }

    public void buildNotficationCustom(@Nullable String current, @Nullable String txCurrent, String battery) {
        Glog.d(TAG, "call buildNotficationCustom()");

        if (mContext == null) {
            return;
        }

        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.activity_custom_notification);

        remoteViews.setImageViewResource(R.id.notification_ico_imageview, R.mipmap.ic_launcher);

        if (current != null) {
            remoteViews.setTextViewText(R.id.notification_current, current);
        } else {
            remoteViews.setTextViewText(R.id.notification_current, "");
        }

        if (txCurrent != null) {
            remoteViews.setTextViewText(R.id.notification_tx_current, txCurrent);
        } else {
            remoteViews.setTextViewText(R.id.notification_tx_current, "");
        }

        if (battery != null) {
            remoteViews.setTextViewText(R.id.notification_battery, battery);
        } else {
            // remoteViews.setTextViewText(R.id.notification_hbm_state, "");
        }

        String name = "AppNotification";
        String id = "AppNotification";
        notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
            Log.i(TAG, mChannel.toString());
            notificationManager.createNotificationChannel(mChannel);
            mNotification = new Notification.Builder(mContext, id)
                    .setChannelId(id)
                    .setOngoing(true)
                    .setCustomContentView(remoteViews)
                    .setSmallIcon(R.mipmap.ic_launcher).build();
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setCustomContentView(remoteViews)
                    .setOngoing(true)
                    .setChannelId(id);
            mNotification = notificationBuilder.build();
        }
        notificationManager.notify(NOTIFICATION_ID_1, mNotification);
    }

    public void cancelNotification(int notificationId) {
        Glog.d(TAG, "call cancelNotification()");
        if (mNotificationManager != null && mContext != null) {
            mNotificationManager.cancel(notificationId);
        }
    }

    public void setmContext(Context context){
        if (mContext != null){
            this.mContext = context;
        }
    }

}
